auth.js ➔ getIsAuthenticated   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 0
1
/*eslint no-undef: 0*/
2
3
import { getStorageItem, setStorageItem, removeStorageItem } from './storage';
4
5
import { isUndefined } from 'lodash';
6
7
if (isUndefined(OAUTH_CLIENT_ID)) {
0 ignored issues
show
Bug introduced by
The variable OAUTH_CLIENT_ID seems to be never declared. If this is a global, consider adding a /** global: OAUTH_CLIENT_ID */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
8
  throw new Error('OAUTH_CLIENT_ID must be set.');
9
}
10
11
if (isUndefined(OAUTH_CLIENT_SECRET)) {
0 ignored issues
show
Bug introduced by
The variable OAUTH_CLIENT_SECRET seems to be never declared. If this is a global, consider adding a /** global: OAUTH_CLIENT_SECRET */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
12
  throw new Error('OAUTH_CLIENT_SECRET must be set.');
13
}
14
15
/**
16
 *
17
 * @param {Object} nextState
18
 * @param {function} replace
19
 */
20
export function requireAuth(nextState, replace) {
21
  if (!getIsAuthenticated()) {
22
    replace('login');
23
  }
24
}
25
26
/**
27
 *
28
 * @param {Object} nextState
29
 * @param {function} replace
30
 */
31
export function requireGuest(nextState, replace) {
32
  if (getIsAuthenticated()) {
33
    replace('/');
34
  }
35
}
36
37
/**
38
 *
39
 * @param {Object} session
40
 */
41
export function setSession(session) {
42
  setStorageItem('auth_session', session);
43
}
44
45
/**
46
 *
47
 * @param {Object} user
48
 */
49
export function setUser(user) {
50
  setStorageItem('auth_user', user);
51
}
52
/**
53
 *
54
 * @returns {Object}
55
 */
56
export function getSession() {
57
  return getStorageItem('auth_session');
58
}
59
/**
60
 *
61
 * @returns {Object}
62
 */
63
export function getUser() {
64
  return getStorageItem('auth_user');
65
}
66
67
/**
68
 *
69
 */
70
export function removeSession() {
71
  removeStorageItem('auth_session');
72
}
73
74
/**
75
 *
76
 */
77
export function removeUser() {
78
  removeStorageItem('auth_user');
79
}
80
81
/**
82
 *
83
 * @returns {boolean}
84
 */
85
export function getIsAuthenticated() {
86
  return Boolean(getAccessToken());
87
}
88
89
/**
90
 *
91
 * @returns {string|null}
92
 */
93
export function getAccessToken() {
94
  return getStorageItem('auth_session.access_token');
95
}
96
97
/**
98
 *
99
 * @returns {string|null}
100
 */
101
export function getRefreshToken() {
102
  return getStorageItem('auth_session.refresh_token');
103
}
104